package com.itheima.Controller;


import com.itheima.annotation.Log;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 分页展示数据
     * @param sqp
     * @return
     */
    @GetMapping
    public Result page(StudentQueryParam sqp){
//        1.接收请求参数
//        2.调用Service层方法
        PageBean students =  studentService.page(sqp);
//        3.返回Result
        return Result.success(students);
    }


    /**
     * 批量删除学员
     * @param ids
     * @return
     */
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
//        1.接收参数
//        2.调用Service层方法
        studentService.delete(ids);

//        3.返回Result
        return Result.success();
    }


    /**
     * 新增学员
     * @param student
     * @return
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Student student){
//        1.接收参数

//        2.调用Service层方法
        studentService.add(student);
//        3.返回Result
        return Result.success();
    }


    /**
     * 学员信息回显
     * @return
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){

//        1.调用Service层方法
        Student student = studentService.getInfo(id);

//        2.返回Result
        return Result.success(student);
    }


    /**
     * 修改学员信息
     * @param student
     * @return
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Student student){

//        1.调用Service层方法
        studentService.update(student);

//        2.返回Result
        return Result.success();
    }


    /**
     * 违纪处理
     * @param id
     * @param score
     * @return
     */
    @Log
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id,@PathVariable Integer score) {
//        1.接收参数
//        2.调用Service层方法
        studentService.violation(id, score);

//        3.返回Result
        return Result.success();
    }
}
